<?php

/*
 * 模仿/index.php
 */
//定义网站根目录,APP目录,DATA目录，ThinkSAAS核心目录
define('IN_TS',true);

define('THINKROOT', '../');
define('THINKAPP', THINKROOT.'/app');
define('THINKDATA',THINKROOT.'/data');
define('THINKSAAS', THINKROOT.'/thinksaas');
define('THINKINSTALL',THINKROOT.'/install');
define('THINKPLUGIN',THINKROOT.'/plugins');

/*
 * 模仿thinksaas/thinksaas.php
 */
//核心配置文件 $TS_CF 系统配置变量
$TS_CF = require_once '../thinksaas/config.php';

//数据库存储SESSION  还有点问题，暂时不要开启
if($TS_CF['session']){
	require_once 'tsSession.php';
	ini_set('session.save_handler', 'user'); 
	session_set_save_handler(
		array('tsSession', 'open'), 
		array('tsSession', 'close'), 
		array('tsSession', 'read'), 
		array('tsSession', 'write'), 
		array('tsSession', 'destroy'), 
		array('tsSession', 'gc') 
	); 
}

/*判断用户是否登录*/
if(!isset($_SESSION)) {
	if(isset($_REQUEST["session_id"])) {
		session_id($_REQUEST["session_id"]);
	}
	session_start();
}

//启动Memcache
if($TS_CF['memcache'] && extension_loaded('memcache')){
	Memcache::connect($TS_CF['memcache']['host'], $TS_CF['memcache']['port']);  
}

//加载基础函数
require_once '../thinksaas/tsFunction.php';

if(!isset($_SESSION['token'])){
	$_SESSION['token'] = sha1(uniqid(mt_rand(),TRUE));
}

//前台用户基本数据,$TS_USER数组
$TS_USER = array(
	'user'		=> isset($_SESSION['tsuser']) ? $_SESSION['tsuser'] : '',
	'admin'		=> isset($_SESSION['tsadmin']) ? $_SESSION['tsadmin'] : '',
); 

//开始处理url路由
reurl();

//记录日志
if($TS_CF['logs']){
	//打印用户日志记录
	userlog($_POST,intval($TS_USER['user']['userid']));
	userlog($_GET,intval($TS_USER['user']['userid']));
}

//系统Url参数变量
//APP专用
$app = isset($_GET['app']) ? $_GET['app'] : 'home';
//Action专用
$ac = isset($_GET['ac']) ? $_GET['ac'] : 'index';
//安装
$install = isset($_GET['install']) ? $_GET['install'] : 'index';
//ThinkSAAS专用
$ts	= isset($_GET['ts']) ? $_GET['ts'] : '';
//Admin管理专用
$mg	= isset($_GET['mg']) ? $_GET['mg'] : 'index';
//Api专用
$api	= isset($_GET['api']) ? $_GET['api'] : 'index';
//plugin专用
$plugin = isset($_GET['plugin']) ? $_GET['plugin'] : '';
//plugin专用
$in = isset($_GET['in']) ? $_GET['in'] : '';

//皮肤
$tstheme = isset($_COOKIE['tsTheme']) ? $_COOKIE['tsTheme'] : 'sample';

tsgpc('app','string');
tsgpc('ac','string');
tsgpc('install','string');
tsgpc('ts','string');
tsgpc('mg','string');
tsgpc('api','string');
tsgpc('plugin','string');
tsgpc('in','string');

//处理html编码
header('Content-Type: text/html; charset=UTF-8');

//数据库配置文件
require_once '../data/config.inc.php';

//连接数据库
require_once '../thinksaas/sql/'.$TS_DB['sql'].'.php';
$db = new MySql($TS_DB);

//加载APP数据库操作类并建立对象
require_once '../thinksaas/tsApp.php';

//除去加载内核运行时间统计开始
$time_start = getmicrotime();

/*
 * 模仿app/index.php
 */
//加载APP应用首页和配置文件
if(is_file('../app/'.$app.'/mobile/'.$ac.'.php')){
	//加载系统缓存文件
	$TS_SITE['base'] = fileRead('../data/system_options.php');
	//定义网站URL
	define('SITE_URL', $TS_SITE['base']['site_url']);	
	//设置时区
	date_default_timezone_set($TS_SITE['base']['timezone']);
	
	//加载APP配置缓存文件
	if($app != 'system'){
		
		$TS_APP['options'] = fileRead('../data/'.$app.'_options.php');
		
		if($TS_APP['options']['isenable']=='1' && $ac != 'admin') {
			qiMsg($app."应用关闭，请开启后访问！");
		}
	}
	
	//加载APP配置文件
 	if(is_file('../app/'.$app.'/class.'.$app.'.php')){
		include_once '../app/'.$app.'/config.php';
		include_once '../app/'.$app.'/class.'.$app.'.php';
		$new[$app] = new $app($db);
	} else {
		header("HTTP/1.1 404 Not Found");
		header("Status: 404 Not Found");
		echo 'No APP 404 Page！';
		exit;
	}
	
	//用户自动登录
	if( !isset($TS_USER['user']['userid']) || $TS_USER['user']['userid'] == '' && 
		isset($_COOKIE['ts_email']) && $_COOKIE['ts_email'] && 
		isset($_COOKIE['ts_uptime']) && $_COOKIE['ts_uptime']){
		
		$loginUserNum = $new[$app]->findCount('user_info',array(
			'email'=>$_COOKIE['ts_email'],
			'uptime'=>$_COOKIE['ts_uptime'],
		));
	
		if($loginUserNum > 0){
			
			$loginUserData = $new[$app]->find('user_info',array(
				'email'=>$_COOKIE['ts_email'],
			),'userid,username,areaid,path,face,count_score,isadmin,uptime');
			
			//用户session信息
			$_SESSION['tsuser']	= $loginUserData;
			$TS_USER = array(
				'user'		=> $_SESSION['tsuser'],
			);

		}
	}
	
	//在执行action之前加载
	doAction('beforeAction');
	
	//全站通用数据加载
	if(is_file('../app/common.php'))
		include '../app/common.php';
	
	//开始执行APP action
	if(is_file('../app/'.$app.'/mobile/common.php'))
		include '../app/'.$app.'/mobile/common.php';
	
	include '../app/'.$app.'/mobile/'.$ac.'.php';
}
?>
